#include <iostream>
#include "dal_dmabufheap/DmaBufHeapManager.hpp"

int main(int argc, char* argv[])
{
    if (argc != 3) {
        std::cerr << "Usage: " << argv[0] << " <size> <global_fd>" << std::endl;
        return 1;
    }

    bool ret;
    int size = std::stoi(argv[1]);
    int global_fd = std::stoi(argv[2]);

    dal_dmabufheap::DmaBufHeapManager* h = dal_dmabufheap::DmaBufHeapManager::GetInstance();
    dal_dmabufheap::DmaBufHeapManager::Block* b = new dal_dmabufheap::DmaBufHeapManager::Block();
    b->global_fd = global_fd;
    b->size = size;

    ret = h->GetMappedBlock(b);
    if (!ret) {
        std::cerr << "GetMappedBlock failed" << std::endl;
        return 1;
    }

    char start[5] = {};
    ret = h->SyncAndReadBlock(b, 0, start, 5);
    if (!ret) {
        std::cerr << "SyncAndReadBlock failed" << std::endl;
        return 1;
    }

    char end[5] = {};
    ret = h->SyncAndReadBlock(b, b->size - 5, end, 5);
    if (!ret) {
        std::cerr << "SyncAndReadBlock failed" << std::endl;
        return 1;
    }

    std::cout << "SyncAndReadBlock succeed, in block, first 5 bytes: ";
    for (int i = 0; i < 5; i++) {
        std::cout << start[i];
    }
    std::cout << ", last 5 bytes: ";
    for (int i = 0; i < 5; i++) {
        std::cout << end[i];
    }
    std::cout << std::endl;
    h->DestroyBlock(b);
}
